<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['send_status']=$this->send_status;
$return['status']=$return['status2']=$this->order_status;
$return['buytype']=$this->buy_types;
$return['sendtype']=$this->send_types;
$return['paytype']=get_pay_types();
$return['paytype']['peerpay']='找人代付';
$return['ispay']=array('0'=>'未支付_No pay','1'=>'已支付_Is pay');
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'deleted'=>0,'plugin_name'=>'jy_mfuwu');
    $orderby='id desc';
	$total='';
	if(!empty($_RQ['ordersn'])){
		$params['ordersn like']='%'.trim($_RQ['ordersn']).'%';
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	if(isset($_RQ['send_status'])){
		$params['send_status']=intval($_RQ['send_status']);
		$return['params']['send_status']=$_RQ['send_status'];
	}
	if(isset($_RQ['status'])){
		if($_RQ['status']==15){
			$params['status']=array(11,12,13,14,15,16);
		}else{
			$params['status']=intval($_RQ['status']);
		}
		$return['params']['status']=$_RQ['status'];
	}
	if(isset($_RQ['sendtype'])){
		$params['sendtype']=intval($_RQ['sendtype']);
		$return['params']['sendtype']=$_RQ['sendtype'];
	}
	if(isset($_RQ['buytype'])){
		$params['buytype']=intval($_RQ['buytype']);
		$return['params']['buytype']=$_RQ['buytype'];
	}
	if(isset($_RQ['merchid'])){
		$params['merchid']=intval($_RQ['merchid']);
		$return['params']['merchid']=$_RQ['merchid'];
	}
	if(isset($_RQ['mid'])){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$tip='所有订单显示，列表项目只显示第一个！';
	$tip .='</br><red>【注意】：本场景已开启独立项目展示，只显示本场景下的订单！</red>';
	$tip .='</br>【<red>订单时间显示不准确？</red>】：可能服务器的时间不准确，联系管理员同步一下服务器网络时间！';
	$tip .='</br>【<red>不显示派单按钮？</red>】：如果是商户的订单！只能商户自己派单，除非商户申请平台代发！';
	$tip .='</br>【<red>导出的订单是按下单时间？</red>】：因为有些订单未支付，不存在支付时间，只能按下单时间来导出！';
	if($params['status']==1){
		$batch=array(
			array('type'=>'post','url'=>'jy_weishop/order.list/setfa/','title'=>'一键派单'),
		);
	}
	$return['list']=pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$jy_shifu=1;
	foreach($return['list'] as &$item){
		$item['member']=cfc('core')->member_show($item['mid']);
		$item['member']['nickname'] .=' (ID:'.$item['mid'].')';
		$goods=pdo_get('jy_weishop_order_goods',array('orderid'=>$item['id']),array('goodsid','total'));
		$goodsinfo=pdo_get('jy_weishop_goods',array('id'=>$goods['goodsid']),array('title','image','unit'));
		$item['total']=$goods['total'];
		$item['title']=$goodsinfo['title'];
		$item['image']=$goodsinfo['image'];
		$item['unit']=$goodsinfo['unit'];
		$item['expressid']=pdo_getcolumn('jy_weishop_order_express',array('orderid'=>$item['id']),'id');
		$item['isjy_shifu']=$jy_shifu;
		$address=$item['address']?json_decode($item['address'],true):$item['addressid'];
		if($address){
			$item['realname']=$address['name']?:$item['realname'];
			$item['address']=model('core/member')->get_address_str($address);
		}
		if($item['sendtype']==1 && $item['is_daifa']==1){
			$item['sendtype']='daifa';
		}
		$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
		$item['can_refund']=model('jy_weishop/order/refund')->check_order_rerefund($item['id']);
		$merch=pdo_get('jy_merch_user',array('id'=>$item['merchid'],'uniacid'=>$_SESSION['uniacid']),array('id','merchname','logo','salecate'));
		$item['merchname']=$merch['merchname']?:'';
		$item['merchlogo']=$merch['logo'];
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>array(
			array('op'=>'list','title'=>'订单管理_Order list'),
		),
		'list'=>array('data'=>'list','openvoice'=>true,'openvoice_action'=>'jy_weishop/order.list/getnew/','tip'=>$tip,'edit'=>false,'total'=>$total,'psize'=>$psize,'html_to'=>'list_html','list'=>array(
			array('name'=>'ordersn','title'=>'订单号_Order ID','type'=>'string'),
			array('name'=>'member','title'=>'会员信息_Member','type'=>'member'),
			array('name'=>'realname','title'=>'收件人_Realname','type'=>'string'),
			array('type'=>'img_string','title'=>'所属商户_Merch title','img'=>'merchlogo',
			'data'=>array(
				array('title'=>'名称：','name'=>'merchname'),
				)
			),
			array('type'=>'img_string','title'=>'项目名称/数量_Goods','img'=>'image',
			'data'=>array(
				array('title'=>'','name'=>'title'),
				array('title'=>'数量：_num：','name'=>'total','unit'=>'unit')
				)
			),
			array('type'=>'string_more','title'=>'金额/时间_Price',
			'data'=>array(
				array('title'=>'金额：_Price：','name'=>'price'),
				array('title'=>'时间：_Time：','name'=>'createtime')
				)
			),
			array('name'=>'sendtype','title'=>'服务方式','type'=>'radio','options'=>'sendtype'),
			array('type'=>'string_more','title'=>'支付/状态_Is pay',
			'data'=>array(
				array('title'=>'是否支付_Is pay?','name'=>'ispay','type'=>'radio','options'=>'ispay'),
				array('title'=>'支付方式_Pay type','name'=>'paytype','type'=>'radio','options'=>'paytype')
				)
			),
		),
		'action'=>array(
			array('type'=>'modal','op'=>'setstatus','title'=>'设置状态'),
			array('type'=>'href','action'=>'/order.detail/','title'=>'订单详情_Detail'),
			array('op'=>'print','title'=>'打印_Print','type'=>'post'),
			array('type'=>'post','op'=>'shou','title'=>'确认完成','show'=>array('status'=>2)),
			array('type'=>'post','op'=>'refund','message'=>'确定要《发起退款》吗？','title'=>'发起退款_Refund','show'=>array('can_refund'=>true)),
			array('op'=>'shifu','title'=>'发起派单','type'=>'modal','show'=>array('status'=>array(1))),
			
		),'batch'=>$batch,
		),
		'search'=>array(
			array('name'=>'ordersn','width'=>10,'title'=>'订单号_Order ID','type'=>'string'),
			array('name'=>'status','width'=>10,'title'=>'订单状态_Status','type'=>'radio','options'=>'status'),
			array('name'=>'buytype','width'=>10,'title'=>'订单类型_Type','type'=>'radio','options'=>'buytype'),
			array('name'=>'sendtype','width'=>10,'title'=>'服务方式','type'=>'radio','options'=>'sendtype'),
			array('type'=>'choose','width'=>13,'title'=>'所属会员','name'=>'mid','action'=>'core/member.get_member/'),
			array('name'=>'createtime','width'=>15,'title'=>'下单时间','type'=>'time_range'),
		)
	);
	if($params['status']==1){
		$htmls['list']['list'][]=array('name'=>'send_status','title'=>'服务状态','type'=>'radio','options'=>'send_status');
	}else{
		$htmls['list']['list'][]=array('name'=>'status','title'=>'订单状态_status','type'=>'radio','options'=>'status');
	}
	if($_SESSION['os']=='windows'){
		$return['htmls']=$htmls;
	}
	exi($return);
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'deleted'=>0,'plugin_name'=>'jy_mfuwu');
	if(!empty($_RQ['ordersn'])){
		$params['ordersn']=trim($_RQ['ordersn']);
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
	}
	if(isset($_RQ['sendtype'])){
		$params['sendtype']=intval($_RQ['sendtype']);
		$return['params']['sendtype']=$_RQ['sendtype'];
	}
	if(isset($_RQ['buytype'])){
		$params['buytype']=intval($_RQ['buytype']);
		$return['params']['buytype']=$_RQ['buytype'];
	}
	if(isset($_RQ['merchid'])){
		$params['merchid']=intval($_RQ['merchid']);
		$return['params']['merchid']=$_RQ['merchid'];
	}
	if(isset($_RQ['mid'])){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$params['uniacid']=$_SESSION['uniacid'];
	$order=pdo_getall('jy_weishop_order',$params);
	if(count($order)>5000){
		exi('订单数量大于5000，请修改搜索条件！','error');
	}
	$status=$this->order_status;
	$paytype=array('wechat'=>'微信支付','credit'=>'余额支付');
	$ispay=array('0'=>'未支付','1'=>'已支付');
	$ordergoods=array();
	foreach($order as &$v){
		$member=pdo_get('core_members',array('id'=>$v['mid']),array('nickname','mobile'));
		$v['nickname']=$member['nickname'];
		$v['usermobile']=$member['mobile'];	
		if($v['address']){
			$address=json_decode($v['address'],true);
		}else{
			$address=pdo_get('core_address',array('id'=>$v['addressid'],'uniacid'=>$_SESSION['uniacid']));
		}
		$v['address']=model('core/member')->get_address_str($v['addressid']);
		$v['address']=str_replace(',','，',$v['address']);
		$v['address']=str_replace("\n","，",$v['address']);
		$v['remark']=str_replace(',','，',$v['remark']);
		$v['remark']=str_replace("\n","，",$v['remark']);
		$v['mobile']=$address['mobile'];
		$v['realname']=$address['realname']?:$address['name'];
		$v['status']=$status[$v['status']];
		$v['paytype']=$paytype[$v['paytype']];
		$v['ispay']=$ispay[$v['ispay']];
		if(!$v['nickname']){
			$v['nickname']='匿名用户';
		}
		$v['createtime']=date('Y-m-d H:i:s',$v['createtime']);
		$v['paytime']=date('Y-m-d H:i:s',$v['paytime']);
		$v['storetitle']=pdo_getcolumn('jy_weishop_store',array('id'=>$v['storeid']),'title');
		$v['storetitle']=$v['storetitle']?:'无';
		$v['diyformdata']=json_decode($v['diyformdata'],true);
		$v['data2']='';
		if(is_array($v['diyformdata'])){
			foreach($v['diyformdata'] as $dk=>&$diyv){
					$v['data2'] .='【'.$diyv['title']."】：";
					if(is_array($diyv['data']) && $diyv['type']!=7){
						$diyv['data']=$diyv['data'][0].' '.$diyv['data'][1].' '.$diyv['data'][2].' '.$diyv['data'][3];
					}
					if($diyv['type']==7){
						$diyv['data']=$diyv['data']['province'].' '.$diyv['data']['city'].' '.$diyv['data']['district'];
					}
					$v['data2'] .=$diyv['data'];
			}
		}
		$invoice=pdo_get('jy_weishop_order_invoice',array('orderid'=>$v['id']));
		if($invoice){
			$type=$invoice['type']==2?'增值税专用发票':'电子普通发票';
			$v['invoice']='['.$type.'] 抬头：'.$invoice['name'].' 税号：'.$invoice['code'].' 邮箱：'.$invoice['email'];
		}
		$goods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$v['id']),array('goodsid','optionid','total'));
		foreach($goods as $k=>$gv){
			$goodstitle=pdo_getcolumn('jy_weishop_goods',array('id'=>$gv['goodsid']),'title');
			//项目规格
			if($gv['optionid']){
				$specs=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$gv['optionid']),'specs');
				$specs=explode("_",$specs);
				foreach($specs as &$spec){
					$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
				}
				$selespename=implode("，",$specs);
			}else{
				$selespename='无规格';
			}
			$num=$k+1;
			if($k){
				$v=array();
			}
			$v['goodstitle']='【项目'.$num.'】'.$goodstitle;
			$v['selespename']=$selespename;
			$v['total']=$gv['total'];
			$ordergoods[]=$v;
		}
	}
	$header=array(
		'ordersn'=>'订单号',
		'storetitle'=>'所属门店',
		'goodstitle'=>'项目名称',
		'selespename'=>'规格',
		'total'=>'数量',
		'nickname'=>'买家昵称',
		'usermobile'=>'买家电话',
		'price'=>'金额',
		'status'=>'订单状态',
		'ispay'=>'支付状态',
		'paytype'=>'支付方式',
		'paytime'=>'支付时间',
		'realname'=>'联系人姓名',
		'address'=>'联系地址',
		'mobile'=>'联系人电话',
		'invoice'=>'发票信息',
		'remark'=>'备注',
		'data2'=>'表单内容',
	);
	cfc('tools')->export2excel($header,$ordergoods,'商城订单列表');
}
if($_RQ['op']=='setfa'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择订单','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			$this->model('order')->order_send($id);
		}
	}	
	exi('一键派单成功！');
}
if($_RQ['op']=='shifu'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('参数错误！','error');
		}
		if(!$post['shifu_id'] && $post['need_setpeisong']){
			exi('请选择师傅！','error');
		}
		if($post['shifu_id']){
			$params['order_id']=$id;
			$params['shifu_id']=$post['shifu_id'];
			model('jy_shifu/order')->order_sendget($params);
		}else{
			cfc('jinyun_socket',$_SESSION['uniacid'])->send2room('刷新服务订单！','shouhou_order');
			$this->model('order')->order_send($id,$data);
		}
		exi('已派单！','','/order.list/list/all=1');
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_weishop_order',array('id'=>$id),array('id','is_peisong','sendtype','storeid','merchid','addressid','ordersn','remark','price','isdish'));	
	$htmls=array(
			'type'=>'edit',
			'desc'=>'订单派单',
	);
	$item['express_type']=1;
	$tip='已派单！等待师傅上门！';
	$htmls['edit']=array(
			array('type'=>'tip','title'=>$tip),
			array('type'=>'switch','name'=>'need_setpeisong','title'=>'需要指定师傅？','show'=>array('express_type'=>1),'help'=>'不指定，则由师傅自己抢单，或系统指派！'),
			array('type'=>'select','title'=>'指定师傅','name'=>'shifu_id','options'=>'shouhous','option_value'=>'mid','show'=>array('need_setpeisong'=>1),'help'=>'只能指定该门店下面的师傅，或本城市的平台师傅！'),
	);
	$shouhous=array();
	$areas=pdo_getall('jy_shifu_area',array('store_id'=>array($item['storeid'],0)));
	foreach($areas as $v){
			$users=pdo_getall('jy_shifu_member',array('group_id'=>$v['id'],'status'=>1,'online >'=>0),array('mid','title'));
			foreach($users as $uv){
					$shouhous[]=$uv;
			}
	}
	$return['shouhous']=$shouhous;
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='setstatus'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$status=intval($post['status']);
		$id=intval($post['id']);
		$update=array('status'=>$status,'ispay'=>intval($post['ispay']));
		if($status==3){
			$update['finishtime']=time();
		}
		model('jy_weishop/task')->statuscheck($post,$status);
		pdo_update('jy_weishop_order',$update,array('id'=>$id));
		exi('设置成功！','','//list/');
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_weishop_order',array('id'=>$id),array('id','ispay','ordersn','status','storeid','createtime'));	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'设置状态',
	);
	$htmls['edit']=array(
			array('type'=>'string','title'=>'ordersn','name'=>'ordersn','read'=>true),
			array('type'=>'radio','title'=>'状态','name'=>'status','options'=>'status'),
			array('type'=>'radio','title'=>'是否支付','name'=>'ispay','options'=>'ispay'),
	);
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='shou'){
	$id=intval($_RQ['id']);
	model('jy_weishop/order')->order_receive($id);
	exi('已服务','','//list/status=3');
}
if($_RQ['op']=='print'){
	$order_id=intval($_RQ['id']);
	$res=model('jy_weishop/printmsg')->storeprint('',$order_id);
	if($res){
		exi('打印成功');
	}else{
		exi('打印失败,该订单所属门店没有添加打印任务！','error');
	}
}

if($_RQ['op']=='deleted'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'deleted'=>1);
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$v){
		$v['member']=cfc('core')->member_show($v['mid']);
	}
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'订单管理'),
		),
		'list'=>array('data'=>'list','edit'=>false,'del_op'=>'ddel','list'=>array(
			array('name'=>'ordersn','title'=>'订单号','type'=>'string'),
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'price','title'=>'金额','type'=>'string'),
			array('name'=>'status','title'=>'订单状态','type'=>'radio','options'=>'status'),	
			array('name'=>'createtime','title'=>'下单时间','type'=>'time'),
			array('name'=>'ispay','title'=>'是否支付','type'=>'radio','options'=>'ispay'),
			array('name'=>'paytype','title'=>'支付方式','type'=>'radio','options'=>'paytype'),				
		),
		'action'=>array(	
			array('type'=>'href','action'=>'order.detail','title'=>'订单详情'),
		),),
		'search'=>array(
			array('name'=>'order','width'=>15,'title'=>'订单号','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='refund'){
	$id=intval($_RQ['id']);
	if($id){
		$order=pdo_get('jy_weishop_order',array('id'=>$id));	
	}else{
		exi('请选择订单！','error');
	}
	$params=array(
			'type'=>0,
			'orderid'=>intval($order['id']),
			'price'=>$order['price'],
			'content'=>'管理员后台手动替客户申请！',
	);
	model('jy_weishop/order/refund')->refund_create($params);
	exi('申请成功！请耐心等待财务审核！','','list');
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_update('jy_weishop_order',array('deleted'=>1),array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_update('jy_weishop_order',array('deleted'=>1),array('id'=>$id));
			}
		}
	}
	exi('删除成功！','','list');
}
if($_RQ['op']=='ddel'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_weishop_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_weishop_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！','','deleted');
}
if($_RQ['op']=='getnew'){
	$params=array(
		'status'=>1,
		'uniacid'=>$_SESSION['uniacid'],
		'merchid'=>intval($_RQ['merchid']),
		'api_id'=>intval($_RQ['api_id']),
		'api_pluginname'=>$_RQ['api_pluginname']?:'',
	);
	$order=pdo_get('jy_weishop_order',$params);
	if($order['id']){
		exi($order);
	}else{
		exi('none');
	}
}
?>